<template>
  <div>
    <el-form ref="pram" :model="pram" :rules="rules" label-width="100px" @submit.native.prevent>
      <el-form-item label="名称" prop="name">
        <el-input v-model="pram.name" maxlength="150" placeholder="名称" />
      </el-form-item>
      <el-form-item label="链接" prop="url">
        <el-input v-model="pram.url" placeholder="url" />
      </el-form-item>
      <el-form-item label="链接" prop="url">
        <el-radio-group v-model="pram.type">
          <el-radio :label="1">新页面跳转</el-radio>
          <el-radio :label="2">本页跳转</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handlerSubmit('pram')">{{ isCreate===0?'确定':'更新' }}</el-button>
        <el-button @click="close">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import * as roleApi from '@/api/role.js'
import * as messageApi from '@/api/message.js'
import {Debounce} from '@/utils/validate'
import Cookies from "js-cookie";
import { saveJump } from '@/api/other'
export default {
  // name: "edit"
  components: { },
  props: {
    isCreate: {
      type: Number,
      required: true
    },
    editData: {
      type: Object,
      default: () => {
        return { rules: [] }
      }
    },
  },
  data() {
    return {
      constants:this.$constants,
      pram: {
        name:'',
        url:'',
        type:1,
        id: 0,
      },
      rules: {
        name: [{ required: true, message: '请填写名称', trigger: ['blur', 'change'] }],
        url: [{ required: true, message: '请填写链接', trigger: ['blur', 'change'] }],
      },
    }
  },
  mounted() {
    this.initEditData()
  },
  methods: {
    close() {
      this.$emit('hideEditDialog')
    },

    initEditData() {
      if (this.isCreate !== 1) return //编辑
      const { name, url, type, id } = this.editData
      this.pram.name = name
      this.pram.url = url
      this.pram.type = type
      this.pram.id = id
    },
    handlerSubmit:Debounce(function(form) {
      this.$refs[form].validate(valid => {
        if (!valid) return
        const loading=this.$loading({'text':'提交中'});
        saveJump(this.pram).then(res=>{
          this.$message.success('操作成功');
          this.close();
        }).finally(()=>{
          loading.close()
        })
      })
    }),
  }
}
</script>

<style scoped>

</style>
